package com.caldremch.component.widget;

import android.content.Context;
import android.support.annotation.ColorInt;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.caldremch.component.R;
import com.caldremch.component.model.LoadStatus;

/**
 * @author Caldremch
 * @date 2017/4/9
 * @describe 内嵌加载view
 * <p>
 * 1.设置自己的图标(加载动画类型)
 * 2.设置状态文字描述(如)
 **/

public abstract class StatusInnerView extends LinearLayout implements View.OnClickListener {


    private OnStatusViewClick onStatusViewClick;
    private StatusViewListener mStatusViewListener;
    private LinearLayout mLlViewLoadig;
    private View mLlViewNetError;
    private ImageView mIvNetError;
    private TextView mTvNetError;

    public void setmStatusViewListener(StatusViewListener mStatusViewListener) {
        this.mStatusViewListener = mStatusViewListener;
    }

    public StatusInnerView(Context context) {
        this(context, null);
    }

    public StatusInnerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void assignViews(View view) {
        mLlViewLoadig = (LinearLayout) view.findViewById(R.id.ll_view_loadig);
        mLlViewNetError = (LinearLayout) view.findViewById(R.id.ll_view_net_error);
        mIvNetError = (ImageView) view.findViewById(R.id.iv_net_error);
        mTvNetError = (TextView) view.findViewById(R.id.tv_net_error);
    }

    public void setNetErrorTips(String tips) {
        mTvNetError.setText(tips);
    }


    private void init() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.status_inner_view, null);
        assignViews(view);
        mLlViewLoadig.addView(getLoadingView());
        mTvNetError.setText(getNetErrorTips());
        mIvNetError.setImageResource(getNetErrorImage());
        mLlViewNetError.setOnClickListener(this);
        addView(view);
    }

    public void setLoadingBackground(@ColorInt int color) {
        mLlViewLoadig.setBackgroundColor(color);
    }

    /**
     * 设置加载错误时图片的展示
     *
     * @return
     */
    protected abstract int getNetErrorImage();

    /**
     * 设置加载错误时提示的文字
     *
     * @return
     */
    protected abstract String getNetErrorTips();

    /**
     * 设置loading view
     *
     * @return
     */
    protected abstract View getLoadingView();

    //开始加载动画
    public abstract void startLoading();

    //停止加载动画
    public abstract void stopLoading();

    //根据类型进行设置
    public void setStatus(Enum type) {

        if (type.equals(LoadStatus.LOADING)) {
            setVisibility(VISIBLE);
            setViewVisible(mLlViewLoadig, VISIBLE);
            setViewVisible(mLlViewNetError, GONE);
            startLoading();

        } else if (type.equals(LoadStatus.ERROR)) {
            setVisibility(VISIBLE);
            setViewVisible(mLlViewLoadig, GONE);
            setViewVisible(mLlViewNetError, VISIBLE);
            stopLoading();

        } else if (type.equals(LoadStatus.NORMAL)) {
            setVisibility(GONE);
            stopLoading();
            if (mStatusViewListener != null) {
                mStatusViewListener.onFinish();
            }

        }

    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.ll_view_net_error) {
            onStatusViewClick.onNetErrorClick(v);
        }

    }

    public interface OnStatusViewClick {
        void onNetErrorClick(View view);

    }

    public void setOnStatusViewClick(OnStatusViewClick onStatusViewClick) {
        this.onStatusViewClick = onStatusViewClick;
    }


    public interface StatusViewListener {
        /**
         * 加载状态结束
         */
        void onFinish();

    }


    /*设置view的隐藏和显示*/
    public static void setViewVisible(View view, int visible) {

        if (view == null) {
            return;
        }
        if (view.getVisibility() != visible) {
            view.setVisibility(visible);
        }
    }
}
